package com.itheima.controller;

import com.itheima.pojo.File;
import com.itheima.service.FileService;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/file")
@Api(tags = "图片服务相关接口")
@CrossOrigin
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 通过fileIds过去信息接口
     *
     * @param fileIds
     * @param code
     * @return
     */
    @GetMapping("/getPicture")
    @ApiOperation("通过fileIds图片信息接口")
    public String add(@RequestParam String fileIds, @RequestParam String code) {
        try {
            List<File> files = fileService.getPicture(fileIds, code);
            return ResultUtil.result(200, files);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, "通过fileIds获取图片失败");
        }
    }

    /**
     * 多图片上传
     *
     * @param request
     * @param filedatas
     * @return
     */
    @PostMapping("/pictures")
    @ApiOperation("多图片上传")
    public String pictures(HttpServletRequest request, List<MultipartFile> filedatas) {
        System.out.println(filedatas.toString());
        try {
            List<File> files = fileService.pictures(request, filedatas);
            return ResultUtil.result(200,files);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, "图片上传失败");
        }
    }
}
